package com.zjj.lbw.config;

import com.zjj.lbw.interceptor.FeignAuthRequestInterceptor;
import feign.Contract;
import feign.Logger;
import feign.Request;
import feign.auth.BasicAuthRequestInterceptor;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhanglei.zjj
 * @description Feign配置类
 * @date 2023/7/9 21:58
 */
@Configuration
public class FeignConfig {
    /**
     * 日志级别
     *
     * @return
     */
    @Bean
    public Logger.Level feignLoggerLevel() {
        return Logger.Level.HEADERS;
    }

    /**
     * 修改契约配置，支持Feign原生的注解
     *
     * @return
     */
    @Bean
    public Contract feignContract() {
        return new Contract.Default();
    }

    /**
     * 开启Basic认证
     *
     * @return
     */
//    @Bean
    public BasicAuthRequestInterceptor basicAuthRequestInterceptor() {
        return new BasicAuthRequestInterceptor("zjj", "123456");
    }

    /**
     * 自定义拦截器
     *
     * @return
     */
//    @Bean
    public FeignAuthRequestInterceptor feignAuthRequestInterceptor() {
        return new FeignAuthRequestInterceptor();
    }

    /**
     * 通过 Options 可以配置连接超时时间和读取超时时间，Options 的第一个参数是连接的超时时间（ms），
     * 默认值是 2s；第二个是请求处理的超时时间（ms），默认值是 5s
     * @return
     */
//    @Bean
    public Request.Options options() {
        return new Request.Options(2000, 3000);
    }

    @Bean
    public Decoder decoder() {
        return new JacksonDecoder();
    }
    @Bean
    public Encoder encoder() {
        return new JacksonEncoder();
    }
}
